<template>
  <div class="cart-container">
    <h2>我的购物车</h2>
    <div v-if="items.length > 0">
      <div class="cart-items">
        <div v-for="item in items" :key="item.id" class="cart-item">
          <div class="item-image">
            <img :src="item.image" :alt="item.name">
          </div>
          <div class="item-info">
            <h3>{{ item.name }}</h3>
            <p class="description">{{ item.description }}</p>
          </div>
          <div class="item-quantity">
            <button @click="decreaseQuantity(item.id)">-</button>
            <span>{{ item.quantity }}</span>
            <button @click="increaseQuantity(item.id)">+</button>
          </div>
          <div class="item-price">
            ¥{{ (item.price * item.quantity).toFixed(2) }}
          </div>
          <button class="remove-item" @click="removeItem(item.id)">
            ×
          </button>
        </div>
      </div>
      <div class="cart-summary">
        <div class="total-items">
          共 {{ itemCount }} 件商品
        </div>
        <div class="total-price">
          总计: ¥{{ totalPrice.toFixed(2) }}
        </div>
        <button class="checkout-btn" @click="checkout">
          结算
        </button>
      </div>
    </div>
    <div v-else class="empty-cart">
      <p>购物车是空的</p>
      <router-link to="/products">去逛逛</router-link>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useCartStore } from '../store'
import { createOrder } from '../api/mockData'

export default {
  setup() {
    const cartStore = useCartStore()
    const router = useRouter()

    const items = computed(() => cartStore.items)
    const totalPrice = computed(() => cartStore.totalPrice)
    const itemCount = computed(() => cartStore.itemCount)

    const increaseQuantity = (productId) => {
      cartStore.addToCart(
          items.value.find(item => item.id === productId),
          1
      )
    }

    const decreaseQuantity = (productId) => {
      const item = items.value.find(item => item.id === productId)
      if (item.quantity > 1) {
        cartStore.addToCart(item, -1)
      } else {
        cartStore.removeFromCart(productId)
      }
    }

    const removeItem = (productId) => {
      cartStore.removeFromCart(productId)
    }

    const checkout = async () => {
      try {
        await createOrder({
          items: items.value,
          total: totalPrice.value,
          userId: localStorage.getItem('userId')
        })
        cartStore.clearCart()
        router.push('/orders')
      } catch (error) {
        alert('结算失败: ' + error.message)
      }
    }

    return {
      items,
      totalPrice,
      itemCount,
      increaseQuantity,
      decreaseQuantity,
      removeItem,
      checkout
    }
  }
}
</script>

<style scoped>
.cart-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem;
}
.cart-items {
  margin-bottom: 2rem;
}
.cart-item {
  display: flex;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid #eee;
  gap: 1rem;
}
.item-image {
  width: 80px;
}
.item-image img {
  width: 100%;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
}
.item-info {
  flex: 1;
}
.item-info h3 {
  margin: 0;
  font-size: 1.1rem;
}
.description {
  color: #666;
  font-size: 0.9rem;
  margin: 0.5rem 0 0;
}
.item-quantity {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.item-quantity button {
  width: 28px;
  height: 28px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
}
.item-quantity button:hover {
  background: #f5f5f5;
}
.item-price {
  font-weight: bold;
  min-width: 80px;
  text-align: right;
}
.remove-item {
  width: 28px;
  height: 28px;
  border: none;
  background: #ffebee;
  color: #e53935;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
.remove-item:hover {
  background: #ffcdd2;
}
.cart-summary {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 2rem;
  padding: 1rem;
  border-top: 1px solid #eee;
}
.total-items {
  color: #666;
}
.total-price {
  font-weight: bold;
  font-size: 1.2rem;
}
.checkout-btn {
  padding: 0.8rem 2rem;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
}
.checkout-btn:hover {
  background: #3aa876;
}
.empty-cart {
  text-align: center;
  padding: 4rem 0;
}
.empty-cart p {
  font-size: 1.2rem;
  color: #666;
  margin-bottom: 1rem;
}
.empty-cart a {
  color: #42b983;
  text-decoration: none;
}
.empty-cart a:hover {
  text-decoration: underline;
}
</style>
